﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <!--<script src="https://unpkg.com/axios/dist/axios.min.js"></script>-->
    <script src="../js/axios.min.js"></script>

    <script>

        //GET方式1
        //axios.get('http://localhost:5000/Axios/Index?name=zhangsan&age=100').then(function (response) {
        //    console.log(response);
        //    console.log(response.data);
        //}).catch(function (error) {
        //    console.log(error);
        //});;

        //GET方式2
        //axios.get('http://localhost:5000/Axios/Index', {
        //    params: {
        //        name: "李四",
        //        age: 18
        //    }
        //}).then(function (response) {
        //    console.log(response);
        //    console.log(response.data);
        //}).catch(function (error) {
        //    console.log(error);
        //});




        //POST
        //data参数请求
        //Content-Type:application/json
        //axios.post('http://localhost:5000/Axios/Index', { name:"张三", age:18 }).then(function (response) {
        //    console.log(response);
        //}).catch(function (error) {
        //    console.log(error);
        //});

        //Content-Type:application/x-www-form-urlencoded
        //axios.post('http://localhost:5000/Axios/Index', "name=张三&age=18").then(function (response) {
        //    console.log(response);
        //}).catch(function (error) {
        //    console.log(error);
        //});

        //axios.post('http://localhost:5000/Axios/Index', parseParam({name:'李四',age:26})).then(function (response) {
        //    console.log(response);
        //}).catch(function (error) {
        //    console.log(error);
        //});



        //Content-Type: multipart/form-data
        //let data = new FormData();
        //data.append('name', '王五');
        //data.append('age', 22);

        //axios.post('http://localhost:5000/Axios/Index', data).then(function (response) {
        //    console.log(response);
        //}).catch(function (error) {
        //    console.log(error);
        //});;



        function parseParam(json) {
            return Object.keys(json).map(v => {
                return v + "=" + json[v];
            }).join("&");
        }

        //console.log(parseParam({ aa: 1, bb: 2 }))





        //并发请求:在同一时间发送多个不同的请求到后端服务,最后统一处理不同服务的响应结果
        function findAll() {
            return axios.get("http://localhost:5000/Axios/Index?name=zhangsan&age=100");
        }

        function save() {
            return axios.post('http://localhost:5000/Axios/Index', "name=张三&age=18");
        }


        //并行发送
        axios.all([findAll(), save()]).then(
            axios.spread(function (result1, result2) {
                console.log(result1);
                console.log(result2);
            }) //用来统一处理多个并发请求的执行结果
        );//all用来处理并发请求

    </script>

</head>
<body>

</body>
</html>